package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.PlanDraCheckDao;
import com.sugon.entity.PlanDraCheckEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.PlanDraCheckService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 非标零件制作审批Service实现类
 *
 * @author sugon
 * @date 2019-08-13 10:50:10
 */
@Service("planDraCheckService")
public class PlanDraCheckServiceImpl implements PlanDraCheckService {
    @Autowired
    private PlanDraCheckDao planDraCheckDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     *
     * @param id 主键
     * @return
     */
    @Override
    public PlanDraCheckEntity queryObject(String id) {
        return planDraCheckDao.queryObject(id);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<PlanDraCheckEntity> queryList(Map<String, Object> map) {
        return planDraCheckDao.queryList(map);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return planDraCheckDao.queryTotal(map);
    }

    /**
     *
     * @param planDraCheck 实体
     * @return
     */
    @Override
    public int save(PlanDraCheckEntity planDraCheck) {
        planDraCheck.setId(RandomUtil.randomString(32));
        planDraCheck.setDelFlag(1);
        planDraCheck.setCreateUser(ShiroUtils.getUserId());
        planDraCheck.setCreateDate(new Date());
        planDraCheck.setDeptCheckStatus(0);
        planDraCheck.setPeCheckStatus(0);
        planDraCheck.setApplyUser(ShiroUtils.getUserId());
        planDraCheck.setApplyDept(ShiroUtils.getDeptId());
        planDraCheck.setApplyDate(new Date());

        sysProcessInfoService.saveProcessInfo(planDraCheck.getId(), ProcessInfoEnum.POE_FBXJZZ, 0, null);

        int rows =  planDraCheckDao.save(planDraCheck);
        if(rows >0){
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = planDraCheck.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../sys/plandracheck.html?Id="+Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.POE_FBXJZZ,msgEntity,0);
        }
        return rows;
    }

    /**
     *
     * @param planDraCheck 实体
     * @return
     */
    @Override
    public int update(PlanDraCheckEntity planDraCheck) {
        planDraCheck.setUpdateUser(ShiroUtils.getUserId());
        planDraCheck.setUpdateDate(new Date());

        int rows =  planDraCheckDao.update(planDraCheck);
        if(rows >0){
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = planDraCheck.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../sys/plandracheck.html?Id="+Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.POE_FBXJZZ,msgEntity,0);
        }
        return rows;
    }

    /**
     *
     * @param planDraCheck
     * @return
     */
    @Override
    public int deptCheck(PlanDraCheckEntity planDraCheck) {

        planDraCheck.setDeptCheckUser(ShiroUtils.getUserId());
        planDraCheck.setDeptCheckDate(new Date());

        sysProcessInfoService.saveProcessInfo(planDraCheck.getId(), ProcessInfoEnum.POE_FBXJZZ, planDraCheck.getDeptCheckStatus(), null);

        int rows = planDraCheckDao.update(planDraCheck);
        if(rows >0){
            int status = planDraCheck.getDeptCheckStatus();
            if (status == 1) {
                SysMsgEntity msgEntity = new SysMsgEntity();
                String Id = planDraCheck.getId();
                msgEntity.setDataId(Id);
                msgEntity.setIsRead(0);
                msgEntity.setDataUrl("../sys/plandracheck.html?Id=" + Id);
                sysMsgService.addApproveItem(ProcessInfoEnum.POE_FBXJZZ, msgEntity, 1);
            }
        }
        return rows;
    }

    /**
     *
     * @param planDraCheck
     * @return
     */
    @Override
    public int peCheck(PlanDraCheckEntity planDraCheck) {

        planDraCheck.setPeCheckUser(ShiroUtils.getUserId());
        planDraCheck.setPeCheckDate(new Date());

        sysProcessInfoService.saveProcessInfo(planDraCheck.getId(), ProcessInfoEnum.POE_FBXJZZ, planDraCheck.getDeptCheckStatus(), null);

        saveSysMsgEntity(planDraCheck.getId(), 2);

        int row =  planDraCheckDao.update(planDraCheck);
        if(row >0){
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = planDraCheck.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.POE_FBXJZZ);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return row;
    }

    /**
     *
     * @param Id
     * @param status
     */
    public void saveSysMsgEntity(String Id, Integer status){
        SysMsgEntity sysMsgEntity = new SysMsgEntity();
        if(2 == status){
            //审核不通过，消息流程提醒结束
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.POE_FBXJZZ);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        }else{
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataUrl("../sys/plandracheck.html");
            sysMsgEntity.setType(SysMsgType.APPROVAL_REMIND);
            sysMsgEntity.setUserId(ShiroUtils.getUserId());
            sysMsgEntity.setDept(ShiroUtils.getDeptId());
            sysMsgEntity.setTitle(ProcessInfoEnum.POE_FBXJZZ.getDesc());
            sysMsgEntity.setDataType(ProcessInfoEnum.POE_FBXJZZ);
        }
        sysMsgService.allMsgData(sysMsgEntity);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return planDraCheckDao.delete(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return planDraCheckDao.deleteBatch(ids);
    }
}
